#!/usr/bin/env python
# -*- coding:utf-8 -*-
from stark.service.stark import StarkConfig, get_choice_text, get_m2m_text, StarkModelForm, Option
from stark.forms.widgets import StarkRadioSelect, StarkCheckboxSelectMultiple
from teach import models
from django.utils.safestring import mark_safe
from django.conf.urls import url
from django.shortcuts import render
from django.urls import reverse


class QuestionLibraryModelForm(StarkModelForm):
    exclude_form_control = ['tags', 'level']
    choice_empty_label = ['level', ]

    class Meta:
        model = models.QuestionLibrary
        exclude = ['creator', ]
        widgets = {
            'level': StarkRadioSelect(),
            'tags': StarkCheckboxSelectMultiple()
        }


class QuestionLibraryHandler(StarkConfig):

    def detail(self, row=None, header=False):
        if header:
            return '标题'

        name = '%s:%s' % (self.site.namespace, self.get_url_name('detail'))
        return mark_safe("<a href='%s'>%s</a>" % (reverse(name, kwargs={'pk': row.pk}), row.title,))

    list_display = [detail, get_choice_text('level', '难度'), 'score', get_m2m_text('tags', 'Tag')]
    per_page_size = 30
    model_form_class = QuestionLibraryModelForm
    list_filter = [
        Option('level', is_choice=True),
        Option('tags', is_multi=True),
    ]
    search_list = ['title', ]

    def save(self, form, modify=False):
        if not modify:
            form.instance.creator_id = self.request.rbac_user_info['pk']
        return form.save()

    def extra_url(self):
        urlpatterns = [
            url(r'^(?P<pk>\d+)/detail/', self.wrapper(self.detail_view), name=self.get_url_name('detail')),
        ]
        return urlpatterns

    def detail_view(self, request, pk):
        """
        题目详细
        :param request:
        :param pk:
        :return:
        """
        question = models.QuestionLibrary.objects.filter(pk=pk).first()
        return render(request, 'teach/question_detail.html', {'question': question})


class PrivateQuestionLibraryHandler(StarkConfig):
    def detail(self, row=None, header=False):
        if header:
            return '标题'

        name = '%s:%s' % (self.site.namespace, self.get_url_name('detail'))
        return mark_safe("<a href='%s'>%s</a>" % (reverse(name, kwargs={'pk': row.pk}), row.title,))

    list_display = [detail, get_choice_text('level', '难度'), 'score', get_m2m_text('tags', 'Tag')]
    per_page_size = 30
    model_form_class = QuestionLibraryModelForm
    list_filter = [
        Option('level', is_choice=True),
        Option('tags', is_multi=True),
    ]

    def save(self, form, modify=False):
        if not modify:
            form.instance.creator_id = self.request.rbac_user_info['pk']
        return form.save()

    def get_queryset(self, request, *args, **kwargs):
        return self.model_class.objects.filter(creator_id=self.request.rbac_user_info['pk'])

    def get_change_queryset(self, request, *args, **kwargs):
        return self.model_class.objects.filter(creator_id=self.request.rbac_user_info['pk'])

    def get_delete_queryset(self, request, *args, **kwargs):
        return self.model_class.objects.filter(creator_id=self.request.rbac_user_info['pk'])

    def extra_url(self):
        urlpatterns = [
            url(r'^(?P<pk>\d+)/detail/', self.wrapper(self.detail_view), name=self.get_url_name('detail')),
        ]
        return urlpatterns

    def detail_view(self, request, pk):
        """
        题目详细
        :param request:
        :param pk:
        :return:
        """
        question = models.QuestionLibrary.objects.filter(pk=pk, creator_id=self.request.rbac_user_info['pk']).first()
        return render(request, 'teach/question_detail.html', {'question': question})
